package com.example.boot1;


import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {
    public static void main(String[] args) throws Exception{
        //learnCountDownLatch()
        //线程+泛型
        learnCountDownLatchAndFanxing();
    }

    //CountDownLatch 和泛型
    public static void learnCountDownLatchAndFanxing() throws Exception{
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 1; i <= 6 ; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"\t 国，被灭");
                countDownLatch.countDown();//掉一次减一
            }, CountryEnum.forEach_CountryEnum(i).getRetMessage()).start();
        }
        //当主线程一下子走到这里时，就会阻塞，看倒计时是否为0，为0的时候主线程才继续往下走
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t ******秦，统一华夏");
    }

    //CountDownLatch 原理及使用演示
    public static void learnCountDownLatch() throws Exception{
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 1; i <= 6 ; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"\t 上完晚自习，离开教室");
                countDownLatch.countDown();//掉一次减一
            }, String.valueOf(i)).start();
        }
        //当主线程一下子走到这里时，就会阻塞，看倒计时是否为0，为0的时候主线程才继续往下走
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t ******班长最后锁门走人");
    }


}
